<?php 
namespace Admin\Controller;
class CategoryController extends CommntController{
	/**
	 * 渲染列表模板
	 */
	public function index()
	{
		$category = self::category_cate();
		$this->assign('list',$category['list']);
		$this->assign('page',$category['show']);
		$this->display();
	}

	

	/**
	 * 封装专门获取分类id
	 */
	public static function category_cate()
	{	
		$moder = M('category');
		
		$count = $moder->count();
		$num   = 10;
		$Page  = new \Think\Page($count,$num);
		$list  = $moder->field("*,concat(path,',',id) as bpath")->order('bpath')->limit($Page->firstRow.','.$Page->listRows)->select();
		foreach ($list as $key => $value) {
			$tmp = explode(',',$value['path']);
			$len = count($tmp) - 1;
			$list[$key]['name'] = str_repeat('&nbsp;'.'|----',$len).$value['name'];
		}
		$show  = $Page->show();
		return $arrayName = array('list' => $list, 'show'=>$show);		
	}
	/**
	 * 封装专门获取分类id
	 */
	public static function category_ppd()
	{	
		$moder = M('category');
		$list  = $moder->field("*,concat(path,',',id) as bpath")->order('bpath')->select();
		foreach ($list as $key => $value) {
			$tmp = explode(',',$value['path']);
			$len = count($tmp) - 1;
			$list[$key]['name'] = str_repeat('&nbsp;'.'|----',$len).$value['name'];
		}
		
		return $list;
	}
	/**
	 * 渲染添加模板
	 */
	public function category_add($id = '')
	{	
		$category = self::category_ppd();
		$this->assign('list',$category);
		$this->assign('id',$id);
		$this->display();
	}
	/**
	 * ajax添加
	 */
	public  function category_post()
	{
		
		$data  = $_POST;
		if (!IS_POST) {
			echo "1";die;
		}
		// var_dump($data);die;
		$cate = M('category');
		//查询分类是否存在
        $child = $cate->where(array('name'=>$data['name']))->select();
        
        if(!empty($child)){
            echo "3";die;
        }
		//获取父级分类di参数
		if ($data['pid'] == 0) {
			$data['path'] = "0";
		}else {
			$pid = $cate->where(array('id'=>$data['pid']))->find();//查询
			$data['path'] = $pid['path'].','.$data['pid'];
		}

		if ($cate->add($data)) {
  			$url = U('Category/index');
  			echo "2#".$url;die;
  		}	
	}

	/**
	 * 渲染修改模板
	 */
	public function category_save()
	{
		$id    = $_GET['id'];
		$model = M('category')->where(array('id'=>$id))->find();
		$this->assign('list',self::category_ppd());
		// var_dump($model);die;
		$this->assign('model',$model);
		$this->display();
	}
	/**
	 * 修改
	 */
	public function category_update()
	{
		
		$data  = $_POST;
		if (!IS_POST) {
			echo "1";die;
		}
		$cate = M('category');
		//查询分类是否存在
        $child = $cate->where(array('name'=>$data['name']))->select();
        if(!empty($child)){
            echo "3";die;
        }
		//获取父级分类di参数
		if ($data['pid'] == 0) {
			$data['path'] = "0";
		}else {
			$pid = $cate->where(array('id'=>$data['pid']))->find();//查询
			$data['path'] = $pid['path'].','.$data['pid'];
		}
		if ($cate->save($data)) {
			$url = U('Category/index');
			echo "2#".$url;die;
		}
	}

	/**
	 * 删除
	 */
	public function category_delete($id='')
	{
		$data  = $_GET['id'];
		$dele = D('Category')->where(array('pid'=>$data))->find();
		if($dele){
			// echo "<script>alert('对不起该分类下有子分类')</script>";
			$this->error('对不起该分类下有子分类,不能删除');
		}
		if(D('Category')->where(array('id'=>$data))->delete()){
			$this->success('删除成功');
		}else{
			$this->error('删除失败');
		}	
	}
}
